package com.shaoshaohuo.app.view;
import java.util.Calendar;

import com.shaoshaohuo.app.R;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.PopupWindow;

/**
 * Description:
 */
public class DatePopwindow implements View.OnClickListener {

    private Context context;
    private PopupWindow hintPopWinwow;

    private Button mCancelButton;
    private Button mOkButton;

    private DatePicker mDatePicker;

    private OnSelectDateListener onSelectDateListener;

    public DatePopwindow(Context context){
        this.context = context;
        initView();
    }

    private void initView(){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View contentView = inflater.inflate(R.layout.popwindow_date, null, false);
        hintPopWinwow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, true);
        hintPopWinwow.setOutsideTouchable(true);
        hintPopWinwow.setBackgroundDrawable(new BitmapDrawable());
        hintPopWinwow.setAnimationStyle(R.style.Anim_Dialog_Bottom);

        mDatePicker = (DatePicker) contentView.findViewById(R.id.dp_date);
        
        mOkButton = (Button) contentView.findViewById(R.id.button_ok);
        mCancelButton = (Button) contentView.findViewById(R.id.button_cancel);

        mOkButton.setOnClickListener(this);
        mCancelButton.setOnClickListener(this);
        
        Calendar date =  Calendar.getInstance();
//        date.set(2014, 12, 30);
        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
//        mDatePicker.setMinDate(date.getTimeInMillis());
        mDatePicker.setMaxDate(date.getTimeInMillis());
        date.set(1900, 1, 1);
        mDatePicker.setMinDate(date.getTimeInMillis());
    }

    public void showLocation(View view){
        hintPopWinwow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
    
    public void setShowMode(int i){
    	switch (i) {
		case 1:
			((ViewGroup)((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);  
	        ((ViewGroup)((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); 
			break;
		case 2:
			((ViewGroup)((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);  
			break;
		}
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
        case R.id.button_ok:
            hintPopWinwow.dismiss();
            if(onSelectDateListener != null){
                onSelectDateListener.onSelectDate(mDatePicker.getYear(),mDatePicker.getMonth()+1,mDatePicker.getDayOfMonth());
            }
            break;
        case R.id.button_cancel:
            hintPopWinwow.dismiss();
            break;
        }
    }

    public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener){
        this.onSelectDateListener = onSelectDateListener;
    }

    public interface OnSelectDateListener{
        void onSelectDate(int year, int month, int day);
    }

}
